/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cflp;

/**
 *
 * @author Gustavo Bula
 */
public class Poblacion {
    private int[][] pob;
    private int[] minCosto;
    private int[] fa;
    private double[] fda;
    private double[] prob;
    private int tam;
    
    public Poblacion(Network n, int t, double p){
        this.tam=t;
        generarSolucion(n,p);
        fda();
        encontrarMinimo();
    }
    
    public Poblacion(Network n, int[][] p){
        this.tam=p.length;
        pob=new int[p.length][p[0].length];
        fa =new int[p.length];
        Individuo ind=new Individuo(n);
        for(int i=0;i<p.length;++i){
            for(int j=0;j<p[i].length;++j){
                pob[i][j]=p[i][j];
            }
             fa[i]=ind.totalCost(pob[i]);
        }
         fda();
         encontrarMinimo();
    }
    
    
    public void generarSolucion(Network n, double p){
        pob=new int[tam][n.getNodesNumber()];
        fa=new int[tam];
        Individuo ind=new Individuo(n,p);
        for(int i=0;i<tam;++i){
            pob[i]=ind.generateSolution2();
            fa[i]=ind.totalCost(pob[i]);
        }
    }
    
    
    public void encontrarMinimo(){
        minCosto = new int[pob[0].length];
        int min=0;
        int fm=fa[0];
        for(int i=1;i<pob.length;++i){
            if(fa[i]<fm){
                min=i;
                fm=fa[i];
            }
          }
        System.arraycopy(pob[min],0, minCosto, 0,pob[min].length);
    }
    
    public void fda(){
        prob=new double[pob.length];
        fda=new double[pob.length];
        double totalFunction=0;
        for(int i=0;i<pob.length;++i){
            totalFunction=totalFunction+(1d/fa[i]);
        }
        double probAcum=0;
        for(int i=0;i<pob.length;++i){
            prob[i]=(1d/fa[i])/(totalFunction);
            probAcum=probAcum+prob[i];
            fda[i]=probAcum;
        }
    }
    
    public void imprimir(){
        //for(int i=0;i<population.length;++i){
        //population[i].printSolutionSPEA2();
        //}
        for(int i=0;i<pob.length;++i){
            System.out.println(i+"\t"+bodegas(pob[i])+"\t"+fa[i]);
        }
    }
    
    public String bodegas(int[] i){
        String s="{";
        for(int j=0;j<i.length;++j){
            if(i[j]==1){
                s=s+(j+1)+" ";
            }
        }
        s=s+"}";
        return s;
    }
    
    public static void main(String[] args){
        Network n=new Network("problemas/N50L2U2K75F10.txt");
        Poblacion p=new Poblacion(n,50,0.5);
        p.imprimir();
        Individuo ind=new Individuo(n,0.5);
        int[][] m=ind.servedNodes(p.pob[0]);
        for(int i=0;i<m.length;++i){
            for(int j=0;j<m[i].length;++j){
                System.out.print(m[i][j]+"\t");
            }
            System.out.println(" ");
        }
    }

    public int[][] getPob() {
        return pob;
    }

    public int[] getFa() {
        return fa;
    }

    public double[] getFda() {
        return fda;
    }

    public double[] getProb() {
        return prob;
    }

    public int getTam() {
        return tam;
    }

    public int[] getMin() {
        return minCosto;
    }

}
